home *** CD-ROM | disk | FTP | other *** search
- /* GetFile.c */
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/dos.h>
- #include <sys/iocs.h>
- #include <errno.h>
- #include <unistd.h>
- #include <alloca.h>
-
- #include <network.h>
- #include <socket.h>
-
- #ifdef WEBXPRESSION
- #include "../WebXpression.h"
- extern void show_mouse_cursor (void);
- extern void hide_mouse_cursor (void);
- extern unsigned char hold_online;
- unsigned char from_network;
- #else
- #include "../RoboGet.h"
- extern char QuitCheck (void);
- #endif
-
- #include "../WebCache/WebCache.h"
- #include "../FuncUrl/FuncUrl.h"
-
- extern void Date2Date (char *, struct tm *);
- extern void McPrint (char *);
- extern void McDbPrint (char *);
- extern void McCursorTop (void);
- extern volatile signed char quit_flag;
-
- static int inetd_version;
-
- static char *content_type_str[]=
- {"text/html", "text/html", "text/html", "text/plain", "text/plain",
- "image/gif", "image/jpeg", "image/jpeg", "image/ping", "image/bmp",
- "application/zip", "application/lzh", "application/gzip",
- "application/pdf",
- "application/octet-stream", "text/plain"};
- static char *ext_type_str[]=
- {"HTM", "HTML", "CGI", "TXT", "DOC",
- "GIF", "JPG", "JPEG", "PNG", "BMP",
- "ZIP", "LZH", "TGZ",
- "PDF",
- "DAT", "*/*"};
-
- enum {
- REQ_HEAD,
- REQ_GET
- };
-
-
- int GetFileInit (void)
- {
- if ((inetd_version = _get_version ())< 0) {
- printf ("ìÉÆm : TCP/IP âhâëâCâoé¬ÅφÆôé╡é─éóé▄é╣é±üB\n"
- " âìü[âJâïâtâ@âCâïü^WebCache âtâ@âCâïé╠é▌ë{ùùé¬ë┬ö\é┼é╖üB\n");
- }
- return (0);
- }
-
-
- void HideMouse (void)
- {
- #ifdef WEBXPRESSION
- int sp;
-
- from_network = !0;
- if (hold_online) {
- sp = _iocs_b_super (0);
- hide_mouse_cursor ();
- _iocs_b_super (sp);
- }
- #endif
- }
-
- void ShowMouse (void)
- {
- #ifdef WEBXPRESSION
- int sp;
-
- from_network = 0;
- if (hold_online) {
- sp = _iocs_b_super (0);
- show_mouse_cursor ();
- _iocs_b_super (sp);
- }
- #endif
- }
-
-
-
- /* URL é┼ÄwÆΦé╡é╜âtâ@âCâïé≡âlâbâgâÅü[âNé⌐éτĵé┴é─é¡éΘ */
- static signed char GetFromNetwork (HTTPFILE * httpfile, char req_mode)
- {
- int netd;
- struct sockaddr_in addr;
- struct hostent *h;
- char scheme[256], hostname[256], path[256], fname[256], query[256], anchor[256];
- int port;
- char temp_str[1024];
-
- httpfile->content_length = 0;
- *httpfile->content_type = '\0';
- httpfile->content = NULL;
-
- #define NO_TIMESTAMP 255
- (httpfile->time_stamp).tm_sec = NO_TIMESTAMP;
-
- UrlSplit (httpfile->url, scheme, hostname, path, fname, query, anchor, &port);
-
- /* â\âPâbâgé≡ì∞ɼé╖éΘ */
- if ((netd = socket (AF_INET, SOCK_STREAM, 0)) < 0) {
- McPrint ("üª â\âPâbâgé¬ì∞ɼé┼é½é▄é╣é±é┼é╡é╜\n");
- return (-1);
- }
- memset (&addr, 0, sizeof (addr)); /* 0 é┼ûäé▀éΘ */
-
- addr.sin_family = AF_INET; /* INETâhâüâCâôé≡ÄwÆΦ */
- addr.sin_port = htons (port); /* http é═â|ü[âg 80ö╘ */
- /* âzâXâgû╝(www.xxx.co.jp) é≡ IP âAâhâîâX(int)é╔ò╧è╖ */
- if ((h = gethostbyname (hostname)) == NULL) {
- McPrint ("üªâhâüâCâôû╝é¬é▌é┬é⌐éΦé▄é╣é±\n");
- return (-1);
- }
- addr.sin_addr.s_addr = *(long *) h->h_addr;
-
- /* æèÄΦɵé╔É┌æ▒é╖éΘ */
- if (req_mode == REQ_HEAD)
- McDbPrint ("âwâbâ_é≡ĵô╛é╡é▄é╖");
- McPrint (hostname);
- McPrint (" é╔É┌æ▒Æå...");
- if (connect (netd, (char *) &addr, sizeof (addr)) < 0) {
- McPrint ("\nüª É┌æ▒é╔Ä╕ösé╡é▄é╡é╜\n");
- return (-1);
- }
- McCursorTop ();
- McPrint (hostname);
- McPrint (" é╔É┌æ▒é╡é▄é╡é╜\n");
-
- if (req_mode == REQ_HEAD)
- sprintf (temp_str, "HEAD %s%s%s HTTP/1.0\r\n", path, fname, query);
- else
- sprintf (temp_str, "GET %s%s%s HTTP/1.0\r\n", path, fname, query);
- McDbPrint (temp_str);
- write_s (netd, temp_str, strlen (temp_str));
- /* write_s (netd, "User-Agent: WebXpression / ver0.01 (X68000)\r\n", 48); */
- strcpy (temp_str, "Accept: */*\r\n");
- write_s (netd, temp_str, strlen (temp_str));
- sprintf (temp_str, "Host: %s\r\n", hostname);
- write_s (netd, temp_str, strlen (temp_str));
- write_s (netd, "\r\n", 2);
-
- McPrint ("âîâXâ|âôâXé≡æ╥é┐é▄é╖...");
-
- /* âwâbâ_é≡éPìsé├é┬ô╟é▌ì₧é▐ */
- recvline (netd, temp_str, 1024);
- if ((strncmp (temp_str, "HTTP/1.0 200", 12))
- && (strncmp (temp_str, "HTTP/1.1 200", 12))) {
- McDbPrint (temp_str);
- close_s (netd); /* É┌æ▒é╠É╪Æf */
- return (-1);
- }
- McCursorTop (); /* "âîâXâ|âôâXé≡æ╥é┐é▄é╖"é≡Å┴ïÄ */
-
- /* ï≤ìsüiâwâbâ_é╠ÅIù╣üjé¬ùêéΘé▄é┼âïü[âv */
- while (recvline (netd, temp_str, 1024), strlen (temp_str) > 3) {
- char temp_entity[256];
-
- McDbPrint ("HEAD > ");
- McDbPrint (temp_str);
-
- sscanf (temp_str, "%s", temp_entity);
- /* âwâbâ_û╝é═æσò╢ÄÜü^żò╢ÄÜé≡ïµò╩é╡é╚éó */
- if (!stricmp (temp_entity, "Content-Type:")) {
- sscanf (temp_str + 14, "%s", httpfile->content_type);
- }
- if (!stricmp (temp_entity, "Content-Length:")) {
- sscanf (temp_str + 16, "%d", &httpfile->content_length);
- }
- if (!stricmp (temp_entity, "Last-Modified:")) {
- Date2Date (temp_str + 15, &(httpfile->time_stamp));
- }
- }
- if (*httpfile->content_type == '\0')
- strcpy (httpfile->content_type, "application/octet-stream");
-
-
- if (req_mode == REQ_HEAD) {
- close_s (netd); /* É┌æ▒é╠É╪Æf */
-
- if ((httpfile->time_stamp).tm_sec == NO_TIMESTAMP) {
- time_t t;
- t = time (NULL);
- httpfile->time_stamp = *localtime (&t);
- }
- McCursorTop ();
- McPrint ("âwâbâ_é≡ĵô╛é╡é▄é╡é╜\n");
- } else {
- FILE *fp;
- int read_size = 0, s;
- void *r = NULL;
- int alloc_size, alloc_left;
- int lap_time;
- char cache_fname[256];
-
- #define YOYUU 4096
- if (httpfile->content_length) {
- alloc_size = httpfile->content_length + YOYUU;
- } else {
- /* 12MB ê╚Åπèmò█é┼é½é╚éóéµü[é± */
- alloc_size = ((int) _dos_malloc (-1)) & 0x00fffffe - YOYUU;
- }
- if (((int) (httpfile->content = _dos_malloc (alloc_size))) < 0) {
- McPrint ("üª âüâéâèé¬æ½éΦé▄é╣é±\n");
- close_s (netd); /* É┌æ▒é╠É╪Æf */
- httpfile->content = NULL;
- return (-1);
- }
- r = httpfile->content;
- alloc_left = alloc_size;
- lap_time = _iocs_ontime ();
- quit_flag = QUIT_NON;
- while ((socklen (netd, 0) >= 0) && (quit_flag == QUIT_NON)) {
- s = read_s (netd, r, alloc_left);
- read_size += s;
- r += s;
- if ((alloc_left -= s) <= 0) {
- McPrint ("üª âüâéâèé¬æ½éΦé▄é╣é±\n");
- break;
- }
- #if 1
- if (httpfile->content_length)
- sprintf (temp_str, "Ä≤ÉMÆå %d/%d âoâCâg", read_size, httpfile->content_length);
- else
- sprintf (temp_str, "Ä≤ÉMÆå %d âoâCâg", read_size);
- McCursorTop (); /* âJü[â\âïé≡ìsé╠ɵô¬é╔ */
- McPrint (temp_str);
- #endif
- #ifndef WEBXPRESSION
- quit_flag = QuitCheck ();
- #endif
- }
- if (quit_flag != QUIT_NON){
- shutdown(netd,0); /* Ä≤ÉMé╡é╜âfü[â^é≡Ä≤é»Äµéτé╕üCé╖é╫é─öpèⁿé╖éΘ */
- shutdown(netd,2); /* connection é≡ aborté╖éΘ */
- }
- close_s (netd); /* É┌æ▒é╠É╪Æf */
- lap_time -= _iocs_ontime ();
- McCursorTop (); /* âJü[â\âïé≡ìsé╠ɵô¬é╔ */
-
- #ifndef WEBXPRESSION
- sprintf (temp_str, " Ä≤ÉMÅIù╣üiÄ≤ÉMæ¼ôxé═ %8.1f âoâCâgü^òbé┼é╡é╜üj", (double) read_size / (double) (-lap_time) * 100.0);
- McPrint (temp_str);
- #endif
-
- if (!(httpfile->content_length)) {
- _dos_setblock (httpfile->content, read_size);
- httpfile->content_length = read_size;
- if (alloc_left <= 0) {
- _dos_mfree (httpfile->content);
- httpfile->content = NULL;
- return (-1);
- }
- }
- if (quit_flag == QUIT_NON) {
- time_t t;
- if ((httpfile->time_stamp).tm_sec != NO_TIMESTAMP) {
- t = mktime (&httpfile->time_stamp);
- } else {
- t = time (NULL);
- httpfile->time_stamp = *localtime (&t);
- }
- WCInsertUrl (httpfile, cache_fname);
- if ((fp = fopen (cache_fname, "wb")) != NULL) {
- struct utimbuf u;
- fwrite (httpfile->content, sizeof (char), read_size, fp);
- fclose (fp);
- u.actime = t + 32400;
- u.modtime = t + 32400;
- utime (cache_fname, &u);
- #ifndef WEBXPRESSION
- McPrint (" : âtâ@âCâïé≡ò█æ╢é╡é▄é╡é╜\n");
- #endif
- }
- } else {
- McPrint ("üª ÆåÆfé╡é▄é╡é╜\n");
- _dos_mfree (httpfile->content);
- httpfile->content = NULL;
- }
- }
- return (0);
- }
-
-
-
- /* URL é┼ÄwÆΦé╡é╜âtâ@âCâïé≡âfâBâXâNé⌐éτĵé┴é─é¡éΘ */
- static void GetFromLocal (HTTPFILE * httpfile)
- {
- FILE *fp;
- char scheme[256], hostname[256], path[256], fname[256], query[256], anchor[256];
- int port;
- char temp_str[256], ext[256];
-
- *httpfile->content_type = '\0';
- httpfile->content_length = 0;
-
- UrlSplit (httpfile->url, scheme, hostname, path, fname, query, anchor, &port);
- strcpy (temp_str, hostname);
- strcat (temp_str, path);
- strcat (temp_str, fname);
- if ((fp = fopen (temp_str, "rb")) != NULL) {
- int i;
- char c;
- char *p;
-
- httpfile->content_length = filelength (fileno (fp));
- if ((int) (httpfile->content = _dos_malloc (httpfile->content_length)) > 0) {
- fread (httpfile->content, httpfile->content_length, 1, fp);
- fclose (fp);
-
- /* â}âïâ`âsâèâIâhö±æ╬ë₧üEüEüE */
- ext[0] = '\0';
- p = &fname[0];
- while (c = *p++) {
- if (c == '.') {
- strcpy (ext, p);
- break;
- }
- }
-
- for (i = 0; i < sizeof (ext_type_str) / sizeof (char *)- 1; i++) {
- if (!stricmp (ext, ext_type_str[i]))
- break;
- }
- strcpy (httpfile->content_type, content_type_str[i]);
- /* éáüAâ^âCâÇâXâ^âôâvé╠Åêù¥é▄é╛ */
-
- } else {
- McDbPrint ("GetFromLocal() : ");
- McPrint ("üª : âüâéâèé¬æ½éΦé▄é╣é±\n");
- httpfile->content = NULL;
- }
- } else {
- McDbPrint ("GetFromLocal() : ");
- McPrint ("üª : âìü[âJâïâtâ@âCâï \n");
- McPrint (httpfile->url);
- McPrint (" é¬âIü[âvâôé┼é½é▄é╣é±\n");
- }
- return;
- }
-
-
-
- /* URL é┼ÄwÆΦé╡é╜âtâ@âCâïé≡âLâââbâVâàé⌐éτĵé┴é─é¡éΘ */
- static void GetFromCache (HTTPFILE * httpfile, char *cache_fname)
- {
- FILE *fp;
-
- #ifndef WEBXPRESSION
- if (!strcmp (httpfile->content_type, "text/html"))
- return;
- #endif
- McDbPrint ("âLâââbâVâàé⌐éτ ");
- McDbPrint (cache_fname);
- McDbPrint (" é≡ô╟é▌ì₧é▌é▄é╖\n");
-
- if ((fp = fopen (cache_fname, "rb")) != NULL) {
- if ((int) (httpfile->content = _dos_malloc (httpfile->content_length)) > 0) {
- fread (httpfile->content, httpfile->content_length, sizeof (char), fp);
- } else {
- McPrint ("üª âüâéâèé¬æ½éΦé▄é╣é±\n");
- httpfile->content = NULL;
- }
- fclose (fp);
- } else {
- McPrint ("üª âLâââbâVâàâtâ@âCâï ");
- McPrint (cache_fname);
- McPrint (" é¬âIü[âvâôé┼é½é▄é╣é±\n");
- }
- return;
- }
-
-
-
- /* URL é┼ÄwÆΦé╡é╜âtâ@âCâïé≡ĵé┴é─é¡éΘ */
- int GetFile (HTTPFILE * httpfile)
- {
- char cache_fname[256];
- char head_flag;
-
- McDbPrint ("GetFile() : ");
- McDbPrint (httpfile->url);
- McDbPrint (" é╠Ä≤ÉMé≡èJÄné╡é▄é╖");
-
- httpfile->content = NULL;
-
- switch (WCExist (httpfile, cache_fname)) {
- case WC_NON: /* âLâââbâVâàé╔æ╢ì▌é╡é╚éóÅΩìç */
- McDbPrint (" / WC_NON\n");
- if (inetd_version >= 0) {
- /* TCP/IP âhâëâCâoé¬ÅφÆôé╡é─éóéΘÅΩìç */
- HideMouse ();
- GetFromNetwork (httpfile, REQ_GET);
- ShowMouse ();
- } else {
- McPrint ("üª TCP/IP âhâëâCâoé¬ÅφÆôé╡é─éóé▄é╣é±\n");
- }
- break;
-
- case WC_INCACHE: /* âLâââbâVâàé╔æ╢ì▌é╡üAïNô«îπÅëé▀é─é╠âAâNâZâXé╠ÅΩìç */
- McDbPrint (" / WC_INCACHE\n");
- WCSetAccess (httpfile);
- if (inetd_version >= 0) {
- /* TCP/IP âhâëâCâoé¬ÅφÆôé╡é─éóéΘÅΩìç */
- HTTPFILE *httpfile2; /* âwâbâ_é¬ò╘éΘ */
-
- httpfile2 = alloca (sizeof (HTTPFILE));
- strcpy (httpfile2->url, httpfile->url);
- HideMouse ();
- head_flag = GetFromNetwork (httpfile2, REQ_HEAD); /* âwâbâ_é≡ĵô╛ */
- ShowMouse ();
- if (!head_flag) {
- /* âwâbâ_é¬Äµô╛Åoùêé╜ÅΩìç */
- McDbPrint ("âwâbâ_ĵô╛ɼî≈\n");
- if (difftime (mktime (&httpfile2->time_stamp), mktime (&httpfile->time_stamp)) > (double) 0.0) {
- /* âtâ@âCâïé¬ìXÉVé│éΩé─éóé╜ÅΩìç */
- McDbPrint ("âtâ@âCâïé¬ìXÉVé│éΩé─éóé▄é╖\n");
- HideMouse ();
- GetFromNetwork (httpfile, REQ_GET);
- ShowMouse ();
- if (httpfile->content != NULL) {
- WCDeleteUrl (httpfile->url);
- remove (cache_fname);
- }
- } else {
- /* âtâ@âCâïé¬ìXÉVé│éΩé─éóé╚éóÅΩìç */
- McDbPrint ("âtâ@âCâïé¬ìXÉVé│éΩé─éóé▄é╣é±\n");
- GetFromCache (httpfile, cache_fname);
- }
- } else {
- /* âwâbâ_é¬Äµô╛é┼é½é╚é⌐é┴é╜ÅΩìç */
- GetFromCache (httpfile, cache_fname);
- }
- } else {
- /* TCP/IP âhâëâCâoé¬ÅφÆôé╡é─éóé╚éóÅΩìç */
- GetFromCache (httpfile, cache_fname);
- }
- break;
-
- case WC_INCACHE2: /* âLâââbâVâàé╔æ╢ì▌é╡üAïNô«îπéPë±ê╚ÅπâAâNâZâXé╡é─éóéΘÅΩìç */
- McDbPrint (" / WC_INCACHE2\n");
- GetFromCache (httpfile, cache_fname);
- break;
-
- case WC_LOCAL:
- McDbPrint (" / WC_LOCAL\n");
- GetFromLocal (httpfile);
- break;
- }
-
- if (httpfile->content == NULL)
- return (-1);
-
- return (0);
- }
-